package com.neusoft.neuiotms.dcom.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.dto.MaintainTaskDTO;
import com.neusoft.neuiotms.dcom.model.enums.MaintainTaskSourceEnum;
import com.neusoft.neuiotms.dcom.service.MaintainTaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "维护任务管理")
@RestController
@RequestMapping(value = "/api/w/maintainTask")
public class MaintainTaskController {
    @Autowired
    private MaintainTaskService maintainTaskService;

    @ApiOperation("查询所有维护任务")
    @GetMapping
    public ResultListDTO<MaintainTaskDTO> index() {
        final List<MaintainTaskDTO> DTOs = this.maintainTaskService.findAll();
        final ResultListDTO<MaintainTaskDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有维护任务")
    @GetMapping("/p")
    public PageResultDTO<MaintainTaskDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.maintainTaskService.search(pageable, searchable));
    }

    @ApiOperation("分页查询所有周期性维护任务")
    @GetMapping("/p/period")
    public PageResultDTO<MaintainTaskDTO> searchPeriod(final UserBean operator, final Pageable pageable,
            final Searchable searchable) {
        searchable.put("source", MaintainTaskSourceEnum.period);
        searchable.put("chargeUserId", operator.getUserId());
        return PageResultDTO.success(this.maintainTaskService.search(pageable, searchable));
    }

    @ApiOperation("分页查询所有临时性维护任务")
    @GetMapping("/p/tmp")
    public PageResultDTO<MaintainTaskDTO> searchTmp(final UserBean operator, final Pageable pageable,
            final Searchable searchable) {
        searchable.put("source", MaintainTaskSourceEnum.temporary);
        searchable.put("chargeUserId", operator.getUserId());
        return PageResultDTO.success(this.maintainTaskService.search(pageable, searchable));
    }

    @ApiOperation("创建维护任务")
    @PostMapping
    public ResultDTO<MaintainTaskDTO> create(@RequestBody final MaintainTaskDTO dto) {
        final MaintainTaskDTO result = this.maintainTaskService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "修改维护任务")
    @PutMapping("/{id}")
    public ResultDTO<MaintainTaskDTO> update(@PathVariable final Long id,
            @RequestBody final MaintainTaskDTO dto) {
        final MaintainTaskDTO result = this.maintainTaskService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("维护任务详情")
    @GetMapping("/{id}")
    public ResultDTO<MaintainTaskDTO> details(@PathVariable final Long id) {
        final MaintainTaskDTO dto = this.maintainTaskService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除维护任务")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.maintainTaskService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("导出维护任务查询结果")
    @GetMapping(value = "/export")
    public void downloadQuery(final Searchable searchable, final Pageable pageable,
            final HttpServletResponse response) {

        final Workbook workbook = this.maintainTaskService.downloadQuery(searchable, pageable);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode("维护任务查询结果" + LocalDateTime.now() + ".xlsx", "UTF-8")));
            workbook.write(out);
            out.flush();
        } catch (final Exception e) {
            MaintainTaskController.log.error("维护任务查询结果导出失败", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                MaintainTaskController.log.error("维护任务查询结果导出关闭流失败", e);
            }
        }
    }

}
